package com.kern.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import com.kern.bean.ResponseResult;
import com.kern.entity.XProduct;
import com.kern.service.IProductService;
import com.kern.service.TestJDBCService;
import com.kern.util.ServletUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by caoshi at 9:52 2021/7/29
 */
@RestController
public class DemoController {


    private static final Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private TestJDBCService testJDBCService;

    @Autowired
    private IProductService productService;


    @RequestMapping("/test")
    public JSONObject test() {
        JSONObject resultObj = new JSONObject();

        testJDBCService.testQuery();

        resultObj.put("msg", "ok");
        return resultObj;
    }


    @RequestMapping("/export")
    public ResponseResult exportExcel(HttpServletRequest request, HttpServletResponse response) {

        String fileName = "lj_product.xlsx";

        List<JSONObject> productJSONList = productService.listAllForJSONList();

        Map<String, String> title = new LinkedHashMap<>();
        title.put("id", "编号");
        title.put("name", "产品名称");
        title.put("subCategory", "产品类别");
        title.put("category", "产品子类别");

        // Workbook workbook = productService.write(fileName, productList, title);
        Workbook workbook = productService.write2(fileName, productJSONList, title);

        try {
            ServletUtil.setHttpServletResponse(request, response, fileName);
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            IoUtil.close(out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ResponseResult.ok("success");
    }

    @RequestMapping("/read")
    public ResponseResult readExcel(HttpServletRequest request) {

        MultipartRequest mReq = (MultipartRequest) request;
        MultipartFile excel = mReq.getFile("excel");
        int count = 0;

        Map<String, String> title = new LinkedHashMap<>();
        title.put("编号", "id");
        title.put("产品名称", "name");
        title.put("产品类别", "subCategory");
        title.put("产品子类别", "category");

        try {
            if (excel != null) {
                InputStream input = excel.getInputStream();
                ExcelReader reader = ExcelUtil.getReader(input, "sheet1");
                reader.setHeaderAlias(title);
                List<XProduct> productList = reader.readAll(XProduct.class);
                System.out.println(productList);
                count = productService.save(productList);
                System.out.println(count);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        return ResponseResult.ok("success", count);
    }








}
